package demo1;

import java.util.Arrays;

public class MyArraylist {
    public int[] elem;
    public int usedSize;//0
    //默认容量
    private static final int DEFAULT_SIZE = 5;
    // 将顺序表的底层容量设置为10(构造方法)
    public MyArraylist() {
        this.elem = new int[DEFAULT_SIZE];
    }
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(elem[i]+" ");
        }
    }

    // 新增元素,默认在数组最后新增
    public void add(int data) {
        if (isFull()){
            //扩容
            grow();
        }
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }
    //扩容数组
    private void grow(){
        this.elem = Arrays.copyOf(this.elem,this.elem.length*2);
    }

    /**
     * 判断当前的顺序表是不是满的！
     * @return true:满   false代表空
     */
    public boolean isFull() {
     return this.usedSize == this.elem.length;

    }
    //设置异常方法
    private void checkPosInAdd(int pos) throws PosInAddException{
        if (pos < 0 || pos > this.usedSize){
            throw new PosInAddException();
        }
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        //判断并接收异常
        try {
            checkPosInAdd(pos);
            //判满
            if(isFull()){
                //扩容
                grow();
            }
            for (int i = this.usedSize; i > pos; i--) {
                elem[i] = elem[i - 1];
            }
            this.elem[pos] = data;
            this.usedSize++;

        }catch (PosInAddException e){
            e.printStackTrace();
            System.out.println("接收了PosInAddException异常");
        }
    }

    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(elem[i] == toFind)
            {
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置
    public int indexOf(int toFind) {

        for (int i = 0; i < this.usedSize; i++) {
            if(elem[i] == toFind)
            {
                return i;
            }
        }
        return -1;
    }

    // 获取 pos 位置的元素
    public int get(int pos) {
        try {
            EmptyException();
            checkPosInAdd(pos);
            return this.elem[pos];
        }catch (PosInAddException e){
            e.printStackTrace();
            System.out.println("接收到PosInAddException异常");
        }catch (IsEmptyException e1){
            e1.printStackTrace();
            System.out.println("接收到IsEmptyException异常");
        }
        return -1;

    }
    public void EmptyException() throws IsEmptyException{
        if(isEmpty()){
            throw new IsEmptyException("列表为空！！");
        }
    }

    private boolean isEmpty() {
        return this.usedSize == 0;
    }
    // 给 pos 位置的元素设为【更新为】 value
    public void set(int pos, int value) {
        try {
            checkPosInAdd(pos);

            this.elem[pos] = value;

        }catch (PosInAddException e){
            e.printStackTrace();
            System.out.println("接收到PosInAddException异常");
        }
    }

    /**
     * 删除第一次出现的关键字key
     * @param key
     */
    public void remove(int key) {
        for (int i = 0; i < this.usedSize; i++) {
            if(elem[i] == key){
                for (int j = i; j < this.usedSize - 1; j++) {
                    elem[j] = elem[j+1];
                }
                this.usedSize--;
                return;
            }

        }

    }

    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }

    // 清空顺序表
    public void clear() {
        this.usedSize = 0;
    }
}
